package ar.com.ford.it.fullEconomics.interfaces.strategys;

public class PackedDecimalConverter {   
    
public static long parsePackedDecimal(byte[] pdIn) throws Exception {   
// Convert packed decimal to long   
      final int PlusSign = 0x0C;       // Plus sign   
      final int MinusSign = 0x0D;      // Minus   
      final int NoSign = 0x0F;         // Unsigned   
      final int DropHO = 0xFF;         // AND mask to drop HO sign bits   
      final int GetLO  = 0x0F;         // Get only LO digit   
long val = 0;                    // Value to return    
    
      for(int i=0; i < pdIn.length; i++) {   
         int aByte = pdIn[i] & DropHO; // Get next 2 digits & drop sign bits   
         if(i == pdIn.length - 1) {    // last digit?   
            int digit = aByte >> 4;    // First get digit   
            val = val*10 + digit;   
//            System.out.println("digit=" + digit + ", val=" + val);   
            int sign = aByte & GetLO;  // now get sign   
            if (sign == MinusSign)   
               val = -val;   
            else {   
               // Do we care if there is an invalid sign?   
               if(sign != PlusSign && sign != NoSign)   
                  throw new Exception("OC7");   
            }   
         }else {   
            int digit = aByte >> 4;    // HO first      
            val = val*10 + digit;   
//            System.out.println("digit=" + digit + ", val=" + val);   
            digit = aByte & GetLO;      // now LO   
            val = val*10 + digit;   
//            System.out.println("digit=" + digit + ", val=" + val);   
         }   
      }   
return val;   
} // end parse()   
   // Test the above   
public static void main(String[] args) throws Exception {   
byte[] pd = new byte[] {0x19, 0x2C};             // 192   
System.out.println(PackedDecimalConverter.parsePackedDecimal(pd));   
      pd = new byte[] {(byte)0x98, 0x44, 0x32, 0x3D};  //-9844323   
System.out.println(PackedDecimalConverter.parsePackedDecimal(pd));   
      pd = new byte[] {(byte)0x98, 0x44, 0x32};  //invalid sign   
System.out.println(PackedDecimalConverter.parsePackedDecimal(pd));   
}   
}